﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

namespace BudgetMealsModels.Entities
{
    public class Recipe
    {
        [HiddenInput(DisplayValue=false)]
        public int RecipeID { get; set; }
        [Required]
        [HiddenInput(DisplayValue=false)]
        public int UserCreatedID { get; set; }
        [Required]
        [HiddenInput(DisplayValue = false)]
        public String UserCreated { get; set; }
        [Required]
        [MaxLength(500)]
        public string Name { get; set; }
        [HiddenInput(DisplayValue = false)]
        [InverseProperty("Recipes")]
        public virtual List<Ingredient> Ingredients { get; set; }
        //update so auto gen the rating
        [Required]
        public int ServingSize { get; set; }
        [NotMapped]
        [HiddenInput(DisplayValue = false)]
        public virtual double Cost
        {
            get
            {
                if (Ingredients != null)
                {
                    return (Ingredients.Sum(r => r.Cost)/ServingSize);
                }
                return 0;
            }
        }
        [HiddenInput(DisplayValue = false)]
        [NotMapped]
        public virtual double Rating
        {
            get
            {
                if (Comments != null)
                {
                    var temp = Comments.Where(c => c.RecipeRating != 0);

                    if (temp.Count() > 0)
                        return Math.Round(temp.Average(c => c.RecipeRating));
                }

                return 0;
            }
        }
        [DataType(DataType.MultilineText)]
        [Required]
        public string Instructions { get; set; }
        [DataType(DataType.MultilineText)]
        [Required]
        public string Summary { get; set; }
        public virtual List<Image> ImageGallery { get; set; }
        [HiddenInput(DisplayValue = false)]
        public virtual List<Comment> Comments { get; set; }
        public List<Meal> Appetizers { get; set; }
        public List<Meal> MainCourses { get; set; }
        public List<Meal> Desserts { get; set; }
        public User User { get; set; }
        [MaxLength(50)]
        public String RecipeEthnicity { get; set; }
        [MaxLength(50)]
        public String RecipeType { get; set; }
    }
}
